package com.shui_shui;

import com.shui_shui.compass.Degree;
import com.shui_shui.db.myDB;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

public class CalCompassActivity extends Activity implements OnTouchListener {
    
	private Degree degreeHome;
	final myDB myDb = new myDB(this);
	
	private TextView degreeText;
	private TextView degreeMount;
	
	private ImageView imageHome;
	private ImageView imageCompass;
	
	private Intent newActivity;
	
	private ImageView back;	
	private ImageView help; 	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);        
		setContentView(R.layout.calcompass);
		
		// Menu Image              
		back = (ImageView) findViewById(R.id.back);        
		help = (ImageView) findViewById(R.id.help);    
				
		//  Set Even OnTouch  MenuBar    
		back.setOnTouchListener(this);        
		help.setOnTouchListener(this);
				
		
		imageHome = (ImageView)findViewById(R.id.imageHome);
		imageCompass = (ImageView)findViewById(R.id.imageCompass);
		
		degreeText = (TextView) findViewById(R.id.degreehomevalue);
		degreeMount = (TextView) findViewById(R.id.mounthomevalue);
		
		degreeHome = Degree.getDegree();
		
		int degree = calDegree();
		degreeHome.setCalDegree(degree);
		
		myDb.selectDATABASE_REVISION_TABLE(degree,degreeHome);
		
		degreeText.setText("\" "+ degree +" องศา \"");
		degreeMount.setText("\" "+ degreeHome.getMountDegree() +" \"");
		
		imageCompass.setOnTouchListener(this);
		imageHome.setOnTouchListener(this);
    }
	
	public int calDegree(){
		int degree = degreeHome.getDegreeHomeLift();
		degree += degreeHome.getDegreeHomeCenter();
		degree += degreeHome.getDegreeHomeRight();
		return (int)degree/3;
	}
	
    @Override
    public void onBackPressed() 
    {
    	finish();
    }

	@SuppressWarnings("deprecation")
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {        
		case R.id.imageHome:            
			switch (event.getAction()) {            
			case MotionEvent.ACTION_DOWN:            
				imageHome.setAlpha(150);
				break;            
			case MotionEvent.ACTION_UP:            
				imageHome.setAlpha(255);  
				newActivity = new Intent(CalCompassActivity.this,DesignHomeActivity.class);
				startActivity(newActivity);
				break;            
			default:                
				break;            
			}            
			break;        
		case R.id.imageCompass:            
			switch (event.getAction()) {            
			case MotionEvent.ACTION_DOWN:            
				imageCompass.setAlpha(150);                
				break;            
			case MotionEvent.ACTION_UP:            
				imageCompass.setAlpha(255);	
				newActivity = new Intent(CalCompassActivity.this,CompassShowActivity.class);
				startActivity(newActivity);
				break;            
			default:                
				break;            
			}            
			break;         
		case R.id.back:            
			switch (event.getAction()) {            
			case MotionEvent.ACTION_DOWN:            
				back.setAlpha(150);                
				break;            
			case MotionEvent.ACTION_UP:            
				back.setAlpha(255);	
				this.finish();
				break;            
			default:                
				break;            
			}            
			break;        
		case R.id.help:            
			switch (event.getAction()) {            
			case MotionEvent.ACTION_DOWN:            
				help.setAlpha(150);                
				break;            
			case MotionEvent.ACTION_UP:            
				help.setAlpha(255);                
				break;            
			default:                
				break;            
			}            
			break; 
		}        
		return true;
	}

}
